import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout,
                             QHBoxLayout, QPushButton, QTextEdit, QLabel, QButtonGroup)
from PyQt5.QtCore import Qt


class ResponsiveLayoutDemo(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        # 设置窗口标题和初始大小
        self.setWindowTitle('方块按钮布局示例')
        self.resize(800, 600)

        # 中央部件，用于承载所有布局和控件
        central_widget = QWidget()
        self.setCentralWidget(central_widget)

        # 整体垂直布局
        main_layout = QVBoxLayout(central_widget)

        # 第一行：三个方块型按钮的水平布局
        button_layout = QHBoxLayout()
        self.button_group = QButtonGroup()
        # 设置按钮组为互斥模式，实现单选效果
        self.button_group.setExclusive(True)

        # 创建方块按钮
        btn1 = QPushButton('选项1')
        btn2 = QPushButton('选项2')
        btn3 = QPushButton('选项3')

        # 设置按钮样式为方块型
        self.set_square_button_style(btn1)
        self.set_square_button_style(btn2)
        self.set_square_button_style(btn3)

        # 将按钮添加到按钮组
        self.button_group.addButton(btn1, 1)
        self.button_group.addButton(btn2, 2)
        self.button_group.addButton(btn3, 3)

        # 添加到布局并设置间距
        button_layout.addWidget(btn1)
        button_layout.addWidget(btn2)
        button_layout.addWidget(btn3)
        button_layout.setSpacing(10)  # 按钮之间的间距
        main_layout.addLayout(button_layout)

        # 第二行：输入输出区域的水平布局
        input_output_layout = QHBoxLayout()
        # 输入部分
        input_label = QLabel('输入：')
        self.input_text = QTextEdit()
        input_v_layout = QVBoxLayout()
        input_v_layout.addWidget(input_label)
        input_v_layout.addWidget(self.input_text)
        input_output_layout.addLayout(input_v_layout)
        # 输出部分
        output_label = QLabel('输出：')
        self.output_text = QTextEdit()
        self.output_text.setReadOnly(True)  # 输出区域设为只读
        output_v_layout = QVBoxLayout()
        output_v_layout.addWidget(output_label)
        output_v_layout.addWidget(self.output_text)
        input_output_layout.addLayout(output_v_layout)
        main_layout.addLayout(input_output_layout)

        # 第三行：处理和清除按钮的水平布局（靠右）
        control_layout = QHBoxLayout()
        control_layout.addStretch()  # 拉伸因子，让按钮靠右
        self.process_btn = QPushButton('处理')
        self.clear_btn = QPushButton('清除')
        control_layout.addWidget(self.process_btn)
        control_layout.addWidget(self.clear_btn)
        main_layout.addLayout(control_layout)

        # 按钮点击信号与槽函数连接
        self.process_btn.clicked.connect(self.process_text)
        self.clear_btn.clicked.connect(self.clear_text)
        # 方块按钮点击事件
        self.button_group.buttonClicked.connect(self.on_button_clicked)

        # 默认选中第一个按钮
        btn1.setChecked(True)

    def set_square_button_style(self, button):
        """设置按钮为方块样式"""
        # 设置固定大小使其呈现方块形状
        button.setFixedSize(100, 100)
        # 设置样式表，使其看起来更像方块选择按钮
        button.setStyleSheet("""
            QPushButton {
                background-color: #f0f0f0;
                border: 2px solid #ccc;
                border-radius: 4px;
                font-size: 14px;
            }
            QPushButton:checked {
                background-color: #4a90e2;
                color: white;
                border-color: #357abd;
            }
            QPushButton:hover {
                border-color: #666;
            }
        """)
        # 设置按钮可以被选中
        button.setCheckable(True)

    def on_button_clicked(self, button):
        """处理方块按钮点击事件"""
        print(f"选中了: {button.text()}")

    def process_text(self):
        """处理输入文本并显示到输出区域"""
        input_content = self.input_text.toPlainText()
        # 获取当前选中的按钮文本
        selected_btn = self.button_group.checkedButton()
        if selected_btn:
            input_content = f"[{selected_btn.text()}] {input_content}"
        self.output_text.setPlainText(input_content)

    def clear_text(self):
        """清空输入和输出区域"""
        self.input_text.clear()
        self.output_text.clear()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = ResponsiveLayoutDemo()
    window.show()
    sys.exit(app.exec_())
